package com.situ.thymeleaf.controller;

import com.situ.thymeleaf.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(Model model) {
        model.addAttribute("message", "HelloWorld");

        Student student = new Student(1, "李四", 23, new Date());
        model.addAttribute("student", student);

        Student student1 = new Student(1, "张三1", 23, new Date());
        Student student2 = new Student(2, "张三2", 23, new Date());
        Student student3 = new Student(3, "张三3", 23, new Date());
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        model.addAttribute("list", list);

        return "index";
    }

    @RequestMapping("/data")
    public String data(HttpServletRequest request, HttpSession session) {
        Student student1 = new Student(1, "张三1", 23, new Date());
        request.setAttribute("student1", student1);

        Student student2 = new Student(2, "张三2", 23, new Date());
        session.setAttribute("student2", student2);

        Student student3 = new Student(3, "张三3", 23, new Date());
        ServletContext application = request.getServletContext();
        application.setAttribute("student3", student3);

        return "data";
    }

    @RequestMapping(value = "/set")
    public String set(Model model) {
        Set<String> allNames = new HashSet<String>() ;
        List<Integer> allIds = new ArrayList<Integer>() ;
        for (int x = 0 ; x < 5 ; x ++) {
            allNames.add("boot-" + x) ;
            allIds.add(x) ;
        }
        model.addAttribute("names", allNames) ;
        model.addAttribute("ids", allIds) ;
        model.addAttribute("mydate", new Date()) ;
        return "set" ;
    }
}
